package o;

import android.text.SpannableStringBuilder;
import java.util.Locale;

public final class gu {
    static final gu ˊ = new gu(false, 2, ॱ);
    private static final String ˋ = Character.toString('‏');
    private static final String ˎ = Character.toString('‎');
    static final gu ˏ = new gu(true, 2, ॱ);
    static final gt ॱ = ha.ˏ;
    private final gt ʼ;
    private final boolean ʽ;
    private final int ॱॱ;

    public static final class b {
        private boolean ˊ;
        private gt ˎ;
        private int ˏ;

        public b() {
            ˎ(gu.ˏ(Locale.getDefault()));
        }

        private void ˎ(boolean z) {
            this.ˊ = z;
            this.ˎ = gu.ॱ;
            this.ˏ = 2;
        }

        private static gu ॱ(boolean z) {
            return z ? gu.ˏ : gu.ˊ;
        }

        public gu ˏ() {
            if (this.ˏ == 2 && this.ˎ == gu.ॱ) {
                return ॱ(this.ˊ);
            }
            return new gu(this.ˊ, this.ˏ, this.ˎ);
        }
    }

    static class d {
        private static final byte[] ˏ = new byte[1792];
        private char ʼ;
        private final int ˊ;
        private final boolean ˋ;
        private final CharSequence ˎ;
        private int ॱ;

        static {
            for (int i = 0; i < 1792; i++) {
                ˏ[i] = Character.getDirectionality(i);
            }
        }

        d(CharSequence charSequence, boolean z) {
            this.ˎ = charSequence;
            this.ˋ = z;
            this.ˊ = charSequence.length();
        }

        int ˋ() {
            this.ॱ = 0;
            int i = 0;
            int i2 = 0;
            int i3 = 0;
            while (this.ॱ < this.ˊ && i == 0) {
                switch (ˎ()) {
                    case (byte) 0:
                        if (i3 != 0) {
                            i = i3;
                            break;
                        }
                        return -1;
                    case (byte) 1:
                    case (byte) 2:
                        if (i3 != 0) {
                            i = i3;
                            break;
                        }
                        return 1;
                    case (byte) 9:
                        break;
                    case (byte) 14:
                    case (byte) 15:
                        i3++;
                        i2 = -1;
                        break;
                    case (byte) 16:
                    case (byte) 17:
                        i3++;
                        i2 = 1;
                        break;
                    case (byte) 18:
                        i3--;
                        i2 = 0;
                        break;
                    default:
                        i = i3;
                        break;
                }
            }
            if (i == 0) {
                return 0;
            }
            if (i2 != 0) {
                return i2;
            }
            while (this.ॱ > 0) {
                switch (ˏ()) {
                    case (byte) 14:
                    case (byte) 15:
                        if (i != i3) {
                            i3--;
                            break;
                        }
                        return -1;
                    case (byte) 16:
                    case (byte) 17:
                        if (i != i3) {
                            i3--;
                            break;
                        }
                        return 1;
                    case (byte) 18:
                        i3++;
                        break;
                    default:
                        break;
                }
            }
            return 0;
        }

        int ॱ() {
            this.ॱ = this.ˊ;
            int i = 0;
            int i2 = 0;
            while (this.ॱ > 0) {
                switch (ˏ()) {
                    case (byte) 0:
                        if (i2 != 0) {
                            if (i != 0) {
                                break;
                            }
                            i = i2;
                            break;
                        }
                        return -1;
                    case (byte) 1:
                    case (byte) 2:
                        if (i2 != 0) {
                            if (i != 0) {
                                break;
                            }
                            i = i2;
                            break;
                        }
                        return 1;
                    case (byte) 9:
                        break;
                    case (byte) 14:
                    case (byte) 15:
                        if (i != i2) {
                            i2--;
                            break;
                        }
                        return -1;
                    case (byte) 16:
                    case (byte) 17:
                        if (i != i2) {
                            i2--;
                            break;
                        }
                        return 1;
                    case (byte) 18:
                        i2++;
                        break;
                    default:
                        if (i != 0) {
                            break;
                        }
                        i = i2;
                        break;
                }
            }
            return 0;
        }

        private static byte ˏ(char c) {
            return c < '܀' ? ˏ[c] : Character.getDirectionality(c);
        }

        byte ˎ() {
            this.ʼ = this.ˎ.charAt(this.ॱ);
            if (Character.isHighSurrogate(this.ʼ)) {
                int codePointAt = Character.codePointAt(this.ˎ, this.ॱ);
                this.ॱ += Character.charCount(codePointAt);
                return Character.getDirectionality(codePointAt);
            }
            this.ॱ++;
            byte ˏ = ˏ(this.ʼ);
            if (!this.ˋ) {
                return ˏ;
            }
            if (this.ʼ == '<') {
                return ˊ();
            }
            if (this.ʼ == '&') {
                return ʻ();
            }
            return ˏ;
        }

        byte ˏ() {
            this.ʼ = this.ˎ.charAt(this.ॱ - 1);
            if (Character.isLowSurrogate(this.ʼ)) {
                int codePointBefore = Character.codePointBefore(this.ˎ, this.ॱ);
                this.ॱ -= Character.charCount(codePointBefore);
                return Character.getDirectionality(codePointBefore);
            }
            this.ॱ--;
            byte ˏ = ˏ(this.ʼ);
            if (!this.ˋ) {
                return ˏ;
            }
            if (this.ʼ == '>') {
                return ᐝ();
            }
            if (this.ʼ == ';') {
                return ʼ();
            }
            return ˏ;
        }

        private byte ˊ() {
            int i = this.ॱ;
            while (this.ॱ < this.ˊ) {
                CharSequence charSequence = this.ˎ;
                int i2 = this.ॱ;
                this.ॱ = i2 + 1;
                this.ʼ = charSequence.charAt(i2);
                if (this.ʼ == '>') {
                    return (byte) 12;
                }
                if (this.ʼ == '\"' || this.ʼ == '\'') {
                    char c = this.ʼ;
                    while (this.ॱ < this.ˊ) {
                        CharSequence charSequence2 = this.ˎ;
                        int i3 = this.ॱ;
                        this.ॱ = i3 + 1;
                        char charAt = charSequence2.charAt(i3);
                        this.ʼ = charAt;
                        if (charAt == c) {
                            break;
                        }
                    }
                }
            }
            this.ॱ = i;
            this.ʼ = '<';
            return (byte) 13;
        }

        private byte ᐝ() {
            int i = this.ॱ;
            while (this.ॱ > 0) {
                CharSequence charSequence = this.ˎ;
                int i2 = this.ॱ - 1;
                this.ॱ = i2;
                this.ʼ = charSequence.charAt(i2);
                if (this.ʼ == '<') {
                    return (byte) 12;
                }
                if (this.ʼ == '>') {
                    break;
                } else if (this.ʼ == '\"' || this.ʼ == '\'') {
                    char c = this.ʼ;
                    while (this.ॱ > 0) {
                        CharSequence charSequence2 = this.ˎ;
                        int i3 = this.ॱ - 1;
                        this.ॱ = i3;
                        char charAt = charSequence2.charAt(i3);
                        this.ʼ = charAt;
                        if (charAt == c) {
                            break;
                        }
                    }
                }
            }
            this.ॱ = i;
            this.ʼ = '>';
            return (byte) 13;
        }

        private byte ʻ() {
            while (this.ॱ < this.ˊ) {
                CharSequence charSequence = this.ˎ;
                int i = this.ॱ;
                this.ॱ = i + 1;
                char charAt = charSequence.charAt(i);
                this.ʼ = charAt;
                if (charAt == ';') {
                    break;
                }
            }
            return (byte) 12;
        }

        private byte ʼ() {
            int i = this.ॱ;
            while (this.ॱ > 0) {
                CharSequence charSequence = this.ˎ;
                int i2 = this.ॱ - 1;
                this.ॱ = i2;
                this.ʼ = charSequence.charAt(i2);
                if (this.ʼ != '&') {
                    if (this.ʼ == ';') {
                        break;
                    }
                }
                return (byte) 12;
            }
            this.ॱ = i;
            this.ʼ = ';';
            return (byte) 13;
        }
    }

    public static gu ˏ() {
        return new b().ˏ();
    }

    gu(boolean z, int i, gt gtVar) {
        this.ʽ = z;
        this.ॱॱ = i;
        this.ʼ = gtVar;
    }

    public boolean ॱ() {
        return (this.ॱॱ & 2) != 0;
    }

    private String ॱ(CharSequence charSequence, gt gtVar) {
        boolean ॱ = gtVar.ॱ(charSequence, 0, charSequence.length());
        if (!this.ʽ && (ॱ || ˎ(charSequence) == 1)) {
            return ˎ;
        }
        if (!this.ʽ || (ॱ && ˎ(charSequence) != -1)) {
            return "";
        }
        return ˋ;
    }

    private String ˎ(CharSequence charSequence, gt gtVar) {
        boolean ॱ = gtVar.ॱ(charSequence, 0, charSequence.length());
        if (!this.ʽ && (ॱ || ˏ(charSequence) == 1)) {
            return ˎ;
        }
        if (!this.ʽ || (ॱ && ˏ(charSequence) != -1)) {
            return "";
        }
        return ˋ;
    }

    public CharSequence ˎ(CharSequence charSequence, gt gtVar, boolean z) {
        if (charSequence == null) {
            return null;
        }
        boolean ॱ = gtVar.ॱ(charSequence, 0, charSequence.length());
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        if (ॱ() && z) {
            spannableStringBuilder.append(ˎ(charSequence, ॱ ? ha.ˋ : ha.ˎ));
        }
        if (ॱ != this.ʽ) {
            spannableStringBuilder.append(ॱ ? '‫' : '‪');
            spannableStringBuilder.append(charSequence);
            spannableStringBuilder.append('‬');
        } else {
            spannableStringBuilder.append(charSequence);
        }
        if (z) {
            spannableStringBuilder.append(ॱ(charSequence, ॱ ? ha.ˋ : ha.ˎ));
        }
        return spannableStringBuilder;
    }

    public CharSequence ˋ(CharSequence charSequence) {
        return ˎ(charSequence, this.ʼ, true);
    }

    static boolean ˏ(Locale locale) {
        return hb.ˎ(locale) == 1;
    }

    private static int ˎ(CharSequence charSequence) {
        return new d(charSequence, false).ॱ();
    }

    private static int ˏ(CharSequence charSequence) {
        return new d(charSequence, false).ˋ();
    }
}
